package com.huawei.chengdu.meetingdemo.model;

import android.text.TextUtils;

import com.device.idea.meeting.Addressbook.model.AddrBookNetNormal;
import com.device.idea.meeting.Addressbook.model.AddrBookNetUserInfo;
import com.device.idea.meeting.Addressbook.model.AddrBookNormal;
import com.device.idea.meeting.Addressbook.model.AddrBookProtocol;
import com.device.idea.meeting.Addressbook.model.AddrBookUserInfo;
import com.device.idea.meeting.CallCtrl.model.CallProtocol;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.consts.ConfConst;

public class AddrBookBean {
    /**
     * 本地地址本信息
     */
    private AddrBookUserInfo userInfo;

    /**
     * 网络地址本
     */
    private AddrBookNetUserInfo netInfo;

    /**
     * 是否视频呼叫
     */
    private boolean isVideo = true;

    /**
     * 是否选中
     */
    private boolean isChecked = false;

    /**
     * 搜索地址本时，如果本地和LDAP都搜索不到，就将输入搜索关键字组装成一个临时ldap记录，用该标志位表示
     */
    private boolean isAddHandle = false;

    /**
     * 是否为填充布局数据
     */
    private boolean isFills = false;

    public AddrBookBean(AddrBookUserInfo suerInfo) {
        this.userInfo = suerInfo;
    }

    public AddrBookBean(AddrBookNetUserInfo netInfo) {
        this.netInfo = netInfo;
    }

    public AddrBookUserInfo getUserInfo() {
        return userInfo;
    }

    public boolean isVideo() {
        return isVideo;
    }

    public void setVideo(boolean video) {
        isVideo = video;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public void setAddHandle(boolean addHandle) {
        isAddHandle = addHandle;
    }

    public boolean isAddHandle() {
        return isAddHandle;
    }

    public boolean isFills() {
        return isFills;
    }

    public void setFills(boolean fills) {
        isFills = fills;
    }

    /**
     * 是否为本地地址本
     *
     * @return true 是
     */
    public boolean isLocal() {
        return userInfo != null;
    }

    /**
     * 设置名称
     *
     * @param name 名称
     */
    public void setName(String name) {
        if (isLocal()) {
            userInfo.setName(name);
        } else {
            netInfo.setName(name);
        }
    }

    /**
     * 获取名称
     *
     * @return 名称
     */
    public String getName() {
        if (isLocal()) {
            return userInfo.getName();
        } else {
            return netInfo.getName();
        }
    }

    /**
     * 获取部门名称
     *
     * @return 部门名称
     */
    public String getDepartMent() {
        if (!isLocal()) { // 本地地址本无部门名称
            return netInfo.getDepartment();
        }
        return "";
    }

    /**
     * 获取呼叫带宽
     *
     * @return 呼叫带宽
     */
    public int getCallRate() {
        if (isLocal()) {
            return userInfo.getLineInfo().getCallRate();
        }
        return ConfConst.DFT_CALL_WIDTH; // 网络地址本使用默认带宽
    }

    /**
     * 设置呼叫带宽
     *
     * @param rate 带宽
     */
    public void setCallRate(int rate) {
        if (isLocal()) {
            userInfo.getLineInfo().setCallRate(rate);
        }
    }

    /**
     * 获取会场号码
     *
     * @return 会场号码
     */
    public String getSiteNumber() {
        if (isLocal()) {
            return userInfo.getLineInfo().getSiteNumber();
        } else {
            return netInfo.getLineInfo().getSiteNumber();
        }
    }

    /**
     * 设置号码
     *
     * @param number 号码
     */
    public void setSiteNumber(String number) {
        if (isLocal()) {
            userInfo.getLineInfo().setSiteNumber(number);
        } else {
            netInfo.getLineInfo().setSiteNumber(number);
        }
    }

    /**
     * 获取ip
     *
     * @return ip
     */
    public String getIP() {
        if (isLocal()) {
            return userInfo.getLineInfo().getIp();
        } else {
            return netInfo.getLineInfo().getIp();
        }
    }

    /**
     * 设置ip
     *
     * @param ip ip
     */
    public void setIP(String ip) {
        if (isLocal()) {
            userInfo.getLineInfo().setIp(ip);
        } else {
            netInfo.getLineInfo().setIp(ip);
        }
    }

    /**
     * 获取url
     *
     * @return url
     */
    public String getUrl() {
        if (isLocal()) {
            return userInfo.getLineInfo().getUrl();
        } else {
            return netInfo.getLineInfo().getUrl();
        }
    }

    /**
     * 设置UIl
     *
     * @param url url
     */
    public void setUrl(String url) {
        if (isLocal()) {
            userInfo.getLineInfo().setUrl(url);
        } else {
            netInfo.getLineInfo().setUrl(url);
        }
    }

    /**
     * 获取呼叫号码
     *
     * @return 呼叫号码
     */
    public String getCalleeNumber() {
        if (AddrBookProtocol.SIP.equals(getAddrBookProtocol()) && !TextUtils.isEmpty(getSiteNumber())) {
            return getSiteNumber();
        }
        if (AddrBookProtocol.H323.equals(getAddrBookProtocol()) && !TextUtils.isEmpty(getUrl())) {
            return getUrl();
        }
        if (AddrBookProtocol.IP.equals(getAddrBookProtocol()) && !TextUtils.isEmpty(getIP())) {
            return getIP();
        }
        if (userInfo != null) { // 加入会议输入的号码
            return userInfo.getName();
        }
        return "";
    }

    /**
     * 获取会场类型
     *
     * @return 会场类型 0：普通会场 1：智真会场 2：CT会场 3：虚拟会议室
     */
    public int getDeviceType() {
        int deviceType = 0;
        if (isLocal()) {
            deviceType = userInfo.getDeviceType();
        } else {
            deviceType = netInfo.getDeviceType();
        }
        return deviceType;
    }

    /**
     * 设会场类型
     *
     * @param type 0：普通会场 1：智真会场 2：CT会场
     */
    public void setDeviceType(int type) {
        if (isLocal()) {
            userInfo.setDeviceType(type);
        } else {
            netInfo.setDeviceType(type);
        }
    }

    /**
     * 获取线路类型
     *
     * @return 0 h323 1 sip 2 ip
     */
    public int getLineType() {
        return getAddrBookProtocol().ordinal();
    }

    private AddrBookProtocol getAddrBookProtocol() {
        if (isLocal()) {
            return userInfo.getLineType();
        } else {
            return netInfo.getLineType();
        }
    }

    /**
     * 获取呼叫类型
     *
     * @return 呼叫类型
     */
    public CallProtocol getCallProtocol() {
        AddrBookProtocol addrBookProtocol = getAddrBookProtocol();
        if (AddrBookProtocol.H323.equals(addrBookProtocol)) {
            return CallProtocol.H323;
        }
        if (AddrBookProtocol.SIP.equals(addrBookProtocol)) {
            return CallProtocol.SIP;
        }
        return CallProtocol.AUTO;
    }

    /**
     * 设置线路类型
     *
     * @param type 0 h323 1 sip 2 ip
     */
    public void setLineType(int type) {
        if (isLocal()) {
            userInfo.setLineType(AddrBookProtocol.getProtocol(type));
        } else {
            netInfo.setLineType(AddrBookProtocol.getProtocol(type));
        }
    }

    /**
     * 地址本主叫呼集获取线路信息
     *
     * @return AddrBookNormal
     */
    public AddrBookNormal getAddrBookNormal() {
        if (isLocal()) {
            return userInfo.getLineInfo();
        } else {
            AddrBookNetNormal addrBookNetNormal = netInfo.getLineInfo();
            AddrBookNormal addrBookNormal = new AddrBookNormal();
            addrBookNormal.setCallRate(0); // 填充默认呼叫带宽
            addrBookNormal.setSiteNumber(addrBookNetNormal.getSiteNumber());
            addrBookNormal.setIp(addrBookNetNormal.getIp());
            addrBookNormal.setUrl(addrBookNetNormal.getUrl());
            return addrBookNormal;
        }
    }

    /**
     * 根据会场类型 获取地址本头像
     *
     * @return 头像id
     */
    public int getAvatarId() {
        int type = getDeviceType();
        if (type == 0) { // 设备类型:0：普通会场 1：智真会场 2：CT会场
            return R.drawable.avatar_site;
        }
        if (type == 3) {
            return R.drawable.avatar_cloud_conf;
        }
        return R.drawable.avatar_user;
    }

    /**
     * 邀请入会界面，获取不同类型地址本icon
     *
     * @return drawable Id
     */
    public int getInviteAvatarId() {
        if (isAddHandle) {
            return R.drawable.invite_temporary;
        }
        if (isLocal()) {
            return R.drawable.participant_local;
        }
        return R.drawable.participant_remote;
    }

    /**
     * 根据会场类型 获取类型名称
     *
     * @return 类型名称str id
     */
    public int getSiteType() {
        int strid = 0;
        switch (getDeviceType()) {
            case 0:
                strid = R.string.site_ordinary;
                break;
            case 1:
                strid = R.string.site_telepresence;
                break;
            case 2:
                strid = R.string.site_ct;
                break;
            default:
                strid = R.string.unknown;
                break;
        }
        return strid;
    }

    /**
     * 比较是否为同一地址本
     *
     * @param addrBookBean 地址本
     * @return true 同一个地址本
     */
    public Boolean equals(AddrBookBean addrBookBean) {
        if (addrBookBean == null) {
            return false;
        }
        // 临时地址本
        if (isAddHandle && addrBookBean.isAddHandle) {
            return isSameTempBean(addrBookBean);
        }
        if (userInfo != null && addrBookBean.userInfo != null &&
                userInfo.getId() == addrBookBean.userInfo.getId()) {
            return true;
        }
        if (netInfo != null && addrBookBean.netInfo != null &&
                netInfo.getUid().equals(addrBookBean.netInfo.getUid())) {
            return true;
        }
        return false;
    }

    /**
     * 用于判断两个临时地址本是否相同
     *
     * @param addrBookBean 被比较临时地址本
     * @return equal? T/F
     */
    private Boolean isSameTempBean(AddrBookBean addrBookBean) {
        return getName().equals(addrBookBean.getName());
    }

    @Override
    public String toString() {
        if (userInfo != null) {
            return "userInfo [Id:" + userInfo.getId() + " Name:" + userInfo.getName() + " LineType:"
                    + userInfo.getLineType() + " DeviceType:" + userInfo.getDeviceType() + " IP:"
                    + userInfo.getLineInfo().getIp() + " URI:" + userInfo.getLineInfo().getUrl() + " Number:"
                    + userInfo.getLineInfo().getSiteNumber() + " CallRate:" + userInfo.getLineInfo().getCallRate()
                    + "]";
        }
        if (netInfo != null) {
            return "userInfo [Name:" + netInfo.getName() + " Uid:" + netInfo.getUid() + " LineType:"
                    + netInfo.getLineType() + " DeviceType:" + netInfo.getDeviceType() + " IP:"
                    + netInfo.getLineInfo().getIp() + " URI:" + netInfo.getLineInfo().getUrl() + " Number:"
                    + netInfo.getLineInfo().getSiteNumber() + "]";
        }
        return "null";
    }
}
